//
//  FolderModel.h
//

#import <Cocoa/Cocoa.h>


@class ThumbData;

#define SORT_BY_DATE		1
#define SORT_BY_NAME		2
#define SORT_BY_TYPE		3
#define SORT_BY_THUMBSTATE	4


@interface FolderModel : NSObject 
{
	int 			sortedBy;
	NSString		*path;
	NSMutableArray	*tdList;
	NSLock			*listLock;
	NSLock			*movieLock;

	// We track the number of callers that have called 'instance' that have not yet called
	// 'releaseInstance'. It's used rather than the Objective-C ref count (retain/release)
	// because we want to manage the lifetime in an application specific way.
	int				refCount;

	// Contains ThumbData items that need to be worked on by thumbDataThread
	NSMutableArray	*threadWorkList;
	id				workListLock;		// An NSConditionLock
	BOOL			*pExitThread;

	// These are the ThumbData items actively being worked on
	NSMutableArray	*activeWorkList;

	// The ThumbData items that need to be handled by the main thread.
	NSMutableArray	*mainThreadWorkList;
	NSLock			*mainThreadListLock;

	// The extensions of the movies that failed generating thumbnails on the secondary thread.
	// Used to exclude other movies with the same extension from attempting to generate the
	// thumbnail on the secondary thread, which is a performance improvement.
	NSMutableSet	*excludedMovieExtensions;

	// If non-null, indicates the file currently being operated on.
	NSString		*operationFile;
}


// Either returns an existing FolderModel or creates a new one, initializing it with the contents
// of the folder. Be certain to call 'releaseInstance' rather than [fm release].
+ (FolderModel*) instance:(NSString *)path  sort:(int)sort  stop:(BOOL*)pStopLoading;
+ (void) releaseInstance:(FolderModel*)fm;


- (id) initWithPath:(NSString*) path  sort:(int)sort  stop:(BOOL*)pStopLoading;
- (id) initWithFM:(FolderModel*)fm  sort:(int)sort  stop:(BOOL*)pStopLoading;

- (void) setOperationFile:(NSString*) file;

- (int) sortedBy;

// Returns the directory this FolderModel represents
- (NSString*) path;

// Returns the number of image files in this model
- (unsigned) count;

// Returns the number of images in the queue (for thumb loading)
- (unsigned) queueCount;

- (int) indexOfTD:(ThumbData*)td;

// Returns the ThumbData at 'index'
- (ThumbData*) tdAtIndex:(unsigned) index;

// Adds the given items, then sends FMItemsAddedNotification
- (void) addItems:(NSArray*) thumbArray;

// Removes the given items, then sends FMItemsRemovedNotification
- (void) removeItems:(NSArray*) thumbArray;

// Sends FMItemsUpdatedNotification
- (void) updateItems:(NSArray*) thumbArray;

// Scans the directory for changes, sending notifications as needed.
- (void) refresh;


- (void) thumbDataThread: (id) nada;
- (void) processThumbData:(ThumbData*) td;
- (NSString*) getMovieThumbnailFilename:(ThumbData*) td;
- (NSImage*) getMovieThumbnail:(ThumbData*) td;
- (NSImage*) generateMovieThumbnail:(ThumbData*) td;
- (void) saveMovieThumbnail:(ThumbData*) td;


- (void) thumbsCompleted:(id) nada;


@end


// Notifications

// Sent when items have been added to a FolderModel
extern NSString *FMItemsAddedNotification;

// Sent when items have been removed from a FolderModel
extern NSString *FMItemsRemovedNotification;

// Sent when items have been updated in a FolderModel
extern NSString *FMItemsUpdatedNotification;

// Sent when a thumbnail has become available (a single ThumbData instance is sent)
extern NSString *FMThumbAvailableNotification;

// Sent when all thumbnails have been processed (sent after the initial load
//  as well as after a refresh, if the refresh needed to load/generate thumbs).
extern NSString *FMThumbsCompletedNotification;


// Defines keys for the NSDictionary accompanying FolderModel notifications

// The single data item for all but FMItemsThumbAvailableNotification
#define FM_NOTIFY_ITEMS		@"items"

// The single data item for FMItemsThumbAvailableNotification
#define FM_THUMB_DATA		@"td"
